﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 单例模板用于派生不需要在Unity中绑定GameObject的单例MonoBehaviour

public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T instance;

    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                GameObject obj = new GameObject();
                instance = obj.AddComponent<T>();
                obj.name = typeof(T).Name;
                DontDestroyOnLoad(obj);
            }
            return instance;
        }
        private set { }
    }
}
